﻿using AutoMapper.Internal.Mappers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SmartHotel.Dto;
using SmartHotel.Menu;
using SmartHotel.Role.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace SmartHotel.Role
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class SmartHotelAppUserRoleService : ApplicationService, ISmartHotelAppUserRoleService
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        private IRepository<UserRoleTable, int> repository;
        private IRepository<RoleTable, int> rolereposipory;
        private IRepository<SmartHotel_UserTable, int> userrepository { get; }

        public SmartHotelAppUserRoleService(IRepository<UserRoleTable, int> repository, IRepository<SmartHotel_UserTable, int> userrepository, IRepository<RoleTable, int> rolereposipory)
        {
            this.repository = repository;
            this.userrepository = userrepository;
            this.rolereposipory = rolereposipory;
        }
        /// <summary>
        /// 用户设置角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ResultDto<CreateandUpdateUserRole>> CreateUserRoleAsync(CreateandUpdateUserRole model)
        {
            var date = await repository.InsertAsync(ObjectMapper.Map<CreateandUpdateUserRole, UserRoleTable>(model));
            return new ResultDto<CreateandUpdateUserRole> { Code = Result.Success, Data = date, Message = "添加成功" };
        }
        /// <summary>
        /// 删除用户角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<int>> DeleteUserRoleAsync(int id)
        {
            var date = await repository.FindAsync(id);
            await repository.DeleteAsync(date);
            return new ResultDto<int> { Code = Result.Success, Data = date, Message = "删除成功" };
        }
        /// <summary>
        /// 获取用户角色分页显示+下拉框查询
        /// </summary>
        /// <returns></returns>
        public async Task<PageDto<UserRoleDto>> GetUserRolesAsync(int pageIndex, int pageSize, int userid)
        {
            var userrole = await repository.GetListAsync();
            var user = await userrepository.GetListAsync();
            var role = await rolereposipory.GetListAsync();

            var data = (from u in user
                        join ur in userrole
                       on u.Id equals ur.UserId
                        join r in role
                        on ur.RoleId equals r.Id
                        select new UserRoleDto()
                        {
                            UserId = u.Id,
                            UserName = u.UserName,
                            UserPhone = u.UserPhone,
                            UserType = u.UserType,
                            UserGrowthValue = u.UserGrowthValue,
                            UserNetbalance = u.UserNetbalance,
                            UserGiftbalance = u.UserGiftbalance,
                            UserAvailableintegral = u.UserAvailableintegral,
                            UserSex = u.UserSex,
                            UserBirthday = u.UserBirthday,
                            UserAdressPC = u.UserAdressPC,
                            UserDetailAddress = u.UserDetailAddress,
                            LoginName = u.LoginName,
                            LoginPassword = u.LoginPassword,
                            RoleId = r.Id,
                            RoleName = r.RoleName,
                            RoleContent = r.RoleContent,
                        }).Distinct().ToList();

            var count = data.Count();

            data = data.WhereIf(userid != 0, x => x.UserId == userid).ToList();
            var list = data.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            //var dto = ObjectMapper.Map<List<UserRoleTable>, List<UserRoleDto>>(list);
            return new PageDto<UserRoleDto> { Code = Result.Success, Data = list, Count = count, Message = "显示成功" };
        }

        /// <summary>
        /// 修改用户角色
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ResultDto<CreateandUpdateUserRole>> UpdateUserRoleAsync(int id, CreateandUpdateUserRole model)
        {
            var list = await repository.GetAsync(id);
            list.UserId = model.UserId;
            list.RoleId = model.RoleId;

            var dto = ObjectMapper.Map<UserRoleTable, CreateandUpdateUserRole>(list);
            return new ResultDto<CreateandUpdateUserRole> { Code = Result.Success, Message = "修改成功" };
        }
        /// <summary>
        /// 根据id查询用户角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<UserRoleDto>> GetUserRoleFindId(int id)
        {
            var date = await repository.GetAsync(id);
            var dto = ObjectMapper.Map<UserRoleTable, UserRoleDto>(date);
            return new ResultDto<UserRoleDto> { Code = Result.Success, Data = dto, Message = "查询成功" };
        }
    }
}
